package com.csweimei.rentmanagementtreasure.pop;

import android.content.Context;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.adapter.QuarterAdapter;
import com.csweimei.rentmanagementtreasure.http.entity.QuarterEntity;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.lxj.xpopup.core.AttachPopupView;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;

public class PopQuarterChoose extends AttachPopupView {
    private PopSelClick mPopSelClick;
    private TextView cancel,sure,add_quarters;
    private Context mContext;
    private List<String> mData = new ArrayList<>();

    private Adapter adapter;
    RecyclerView recyclerView;

    private String sel;

    private String adds;

    public PopQuarterChoose(@NonNull Context context, List<String> data, PopSelClick popSelClick) {
        super(context);
        mContext = context;
        mPopSelClick = popSelClick;
        mData.clear();
        mData.addAll(data);
    }


    @Override
    protected int getImplLayoutId() {
        return R.layout.pop_quarters_choose;
    }

    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        getPopupContentView().setLayoutParams(layoutParams);

        cancel = findViewById(R.id.cancel);
        cancel.setText("重置");
        sure = findViewById(R.id.sure);
        add_quarters = findViewById(R.id.add_quarters);
        recyclerView = findViewById(R.id.recycler);

        adapter = new Adapter();
        adapter.addChildClickViewIds(R.id.name);
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            adapter.multipleChoose(position);
            sel = mData.get(position);
        });
        recyclerView.setAdapter(adapter);
        adapter.setList(mData);
        sel = mData.get(0);
        sure.setOnClickListener(v -> {
            mPopSelClick.sel(new PopBean(sel));
            dismiss();
        });

        cancel.setOnClickListener(v -> {
            mPopSelClick.sel(new PopBean("全部"));
            dismiss();
        });

    }

    class Adapter extends BaseQuickAdapter<String, BaseViewHolder> {

        public Adapter() {
            super(R.layout.item_quarter);
        }

        private int index = 0;
        //设置多选。选中取消
        public void multipleChoose(int position) {
            index = position;
            notifyDataSetChanged();  //更新数据源,在这里更新之后,activity里面的数据也会同步更新

        }


        @Override
        protected void convert(@NonNull BaseViewHolder baseViewHolder, String s) {
            baseViewHolder.setText(R.id.name,s);
            baseViewHolder.setBackgroundResource(R.id.name,R.drawable.rect_f6_60);

            if (getItemPosition(s) == index){
                baseViewHolder.setTextColorRes(R.id.name,R.color.white);
                baseViewHolder.setBackgroundResource(R.id.name,R.drawable.rect_main_60);
            }else {
                baseViewHolder.setTextColorRes(R.id.name,R.color.text_c);
                baseViewHolder.setBackgroundResource(R.id.name,R.drawable.rect_f6_60);
            }

        }




    }
}
